//
//  DTLUserLeftViewController.m
//  DTLLiteProject
//
//  Created by Kendama on 2017/7/18.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "LeftViewController.h"
#import "LeftView.h"
#import "MainSlideViewController.h"
#import "UIViewController+LGSideMenuController.h"
#import "HomeViewController.h"
#import "LeftTableViewCell.h"
#import "FeedbackViewController.h"
#import "UserInfoViewController.h"
#import "NewsViewController.h"
#import "WebViewController.h"

@interface LeftViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) LeftView *containerView;
@property(nonatomic,strong) NSArray *dataSource;
@property(nonatomic,strong) NSArray *iconSource;
@property(nonatomic,strong) UIButton *closeButton;

@end

@implementation LeftViewController

#pragma mark - view lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.containerView.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refreshBaseControlValue
{
    self.navigationBar.hidden = YES;
    
    [self updateData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:LGSideMenuControllerDidShowLeftViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:@"updateUserInfo" object:nil];
    self.view.backgroundColor = [UIColor clearColor];
}

#pragma mark - 点击事件
- (void)didClickCloseButton
{
    [self.sideMenuController hideLeftViewAnimated:YES completionHandler:nil];
}

#pragma mark - 事件处理
- (UIViewController *)getRootViewController
{
    MainSlideViewController *mainViewController = (MainSlideViewController *)self.sideMenuController;
    HomeViewController *vc = (HomeViewController *)mainViewController.rootViewController;
    UINavigationController *navigationController = (UINavigationController *)vc;
    return [navigationController.viewControllers lastObject];
}

- (void)updateData
{
    [self.containerView.avatarView sd_setImageWithURL:[NSURL URLWithString:EXRACT_OBJECT(IMAGE)] placeholderImage:imageWithName(@"icon_tacitly")];
    self.containerView.nicknameLabel.text = EXRACT_OBJECT(NICKNAME);
    [self.containerView.tableView reloadData];
}

#pragma mark - 代理
#pragma mark - TableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return px1920Height(200);
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return CGFLOAT_MIN;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    LeftTableViewCell *cell = [LeftTableViewCell dequeueReusableWithTableView:tableView];
    cell.titleLabel.text = self.dataSource[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:self.iconSource[indexPath.row]];
    NSLog(@"消息数%@",EXRACT_OBJECT(NEWSCOUNT));
    if (indexPath.row == 1)
    {
        if (EXRACT_OBJECT(NEWSCOUNT) == nil)
        {
            cell.numberLabel.hidden = YES;
        }
        else
        {
            cell.numberLabel.hidden = NO;
            cell.numberLabel.text = EXRACT_OBJECT(NEWSCOUNT);
        }
        cell.numberLabel.backgroundColor = [UIColor colorWithHexString:@"F06161"];
    }
    else
    {
        cell.numberLabel.backgroundColor = [UIColor whiteColor];
        cell.numberLabel.hidden = YES;
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    [self.sideMenuController hideLeftViewAnimated:YES completionHandler:nil];
//    UIViewController *rootVC = [self getRootViewController];
    UIViewController *rootVC = self;
    if (indexPath.row == 4)
    {
        [[AlertViewManager shareManager] initWithMessage:MyNSLocalizedString(@"是否退出君泽？") cancleButtonTitle:MyNSLocalizedString(@"取消")OtherButtonsArray:@[MyNSLocalizedString(@"确定")] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
            if (buttonIndex == 1)
            {
                SAVE_OBJECT(@"NO", LOGIN);
                REMOVE_OBJECT(UID);
                REMOVE_OBJECT(TOKEN);
                REMOVE_OBJECT(IMAGE);
                [[AppDelegate defaultService]setRootViewController];
            }
        }];
    }
    else if (indexPath.row == 0)
    {
        UserInfoViewController *vc = [[UserInfoViewController alloc] init];
        [rootVC.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 1)
    {
        NewsViewController *vc = [[NewsViewController alloc] init];
        [rootVC.navigationController pushViewController:vc animated:YES];
        REMOVE_OBJECT(NEWSCOUNT);
    }
    else if (indexPath.row == 2)
    {
        FeedbackViewController *vc = [[FeedbackViewController alloc] init];
        [rootVC.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 3)
    {
        WebViewController *vc = [[WebViewController alloc] init];
        vc.webViewType = Instructions;
        [rootVC.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 通知处理

#pragma mark - 初始化
- (NSArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = @[MyNSLocalizedString(@"个人信息"),MyNSLocalizedString(@"消息通知"),MyNSLocalizedString(@"用户反馈"),MyNSLocalizedString(@"使用说明"),MyNSLocalizedString(@"退出登录")];
    }
    return _dataSource;
}

- (NSArray *)iconSource
{
    if(!_iconSource)
    {
        _iconSource = @[@"left_icon_message",@"left_icon_inform",@"left_icon_feedback",@"left_icon_help",@"left_icon_quit"];
    }
    return _iconSource;
}

- (void)masLayoutSubview{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(superView);
        make.width.mas_equalTo(px1080Width(700));
    }];
}

- (LeftView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[LeftView alloc]init];
        [superView addSubview:_containerView];
//        [_containerView.groupingButton addTarget:self action:@selector(didClickGroupingButton) forControlEvents:UIControlEventTouchUpInside];
        _containerView.tableView.delegate = self;
        _containerView.tableView.dataSource = self;
    }
    return _containerView;
}

- (UIButton *)closeButton
{
    if(!_closeButton)
    {
        UIView *superView = self.view;
        _closeButton = [[UIButton alloc]init];
        [superView addSubview:_closeButton];
        [_closeButton addTarget:self action:@selector(didClickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}


@end
